<template>
	<view>
		<view class="goods-list-box">
			<view class="good-list" v-for="(item,index) in goodsList" :key="index" @click="gotoDetail(item)">
				<my-goods :item='item'></my-goods>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					//请求参数对象
					query: '',
					//商品分类id
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				isloading: false,
				goodsList: [],
				total: 0

			};

		},
		//options表示来自页面的参数
		onLoad(options) {
			this.getGoodsList()
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
		},
		methods: {
			async getGoodsList(cb) {
				this.isloading = true
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				//数据获取成功之后调用cb及uni.stopPullDownRefresh()
				cb && cb()
				if (res.meta.status !== 200) uni.$showMsg("数据获取失败")
				this.isloading = false
				//拼接新数据
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total

			},
			gotoDetail(item) {
				uni.navigateTo({
					url: '/subpackage/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			}

		},
		//上拉加载更多
		onReachBottom() {
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.queryObj.total)
				uni.$showMsg('数据加载完毕')
			if (this.isloading) return
			this.queryObj.pagenum++
			this.getGoodsList()
		},
		onPullDownRefresh() {
			this.queryObj.pagenum = 1
			this.total = 0;
			this.isloading = false
			this.goodsList = []
			//cb 是这个函数的参数 
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}


	}
</script>

<style lang="scss">

</style>